/* File name: McBSP0_slave.c

   Can be used for 16-bit transfers or 32-bit transfers.

   12Jan2007 .. initial version .. KM
   09Mar2008 .. slave version from master .. KM 

*/

#include "C5510.h"

static int wsize;

unsigned long McBSP0_slave(unsigned long Value) // Defaults to 16 if Value != 32.
{
    unsigned int high, low;
    
    while((SPCR10&0x0002) == 0);  // check receiver flag
    high = DRR20;
    low  = DRR10;
    while((SPCR20&0x0002) == 0);  // wait until xmtr empty
    DXR20 = Value>>16;
    DXR10 = Value;
    if (wsize == 32) return(((unsigned long)high<<16)+(unsigned)low);
    else return ((unsigned long)low);
}

void McBSP0_slave_setup(int ws)
{
    wsize = ws;

    /* set up McBSP port 0 */

    SPCR10 = 0x0000;  // stop rcvr
    SPCR20 = 0x0000;  // stop xmtr
    if (wsize == 32) RCR10  = 0x00A0;  // 1 32-bit word
    else             RCR10  = 0x0040;  // 1 16-bit word
    RCR20  = 0x0000;  // delay data 0 bit
    if (wsize == 32) XCR10  = 0x00A0;  // 1 32-bit word
    else             XCR10  = 0x0040;  // 1 16-bit word
    XCR20  = 0x0000;  // delay data 0 bit
    SRGR10 = 0x0000;  // low 8 bits divide clock
    SRGR20 = 0x0000;
    MCR10  = 0x0000;
    MCR20  = 0x0000;
    PCR0   = 0x0000;  // use of clock edges

    SPCR10 = 0x0001;  // start rcvr
    SPCR20 = 0x00C1;  // start xmtr
}
